package com.tchintech.pay.view.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.tchintech.pay.R;
import com.tchintech.pay.delegate.LostPassworddelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.net.OkhttpClientTest;
import com.tchintech.pay.utils.RegexUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.sms.SMSSDK;
import cn.jpush.sms.listener.SmscheckListener;
import cn.jpush.sms.listener.SmscodeListener;

/**
 * Created by Administrator on 2017/7/7 0007.
 */

public class LostPasswordActivity extends ActivityPresenter<LostPassworddelegate> implements View.OnClickListener {

    private Map<String, String> map;
    private Handler handler;
    private String phone, password, repPassword, code;
    private TextView tv_code;
    private String url_lostpassword = "PassWord.forget";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setTitle("忘记密码");
        viewDelegate.setVisible();
        tv_code = viewDelegate.getTextView(R.id.password_code);
        viewDelegate.setOnClickListener(this, R.id.lost_bt_confirm, R.id.password_rel,R.id.iv_back);
        handler();
    }

    private void handler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                hideProgressDialog();
                switch (msg.what) {
                    case HttpCode.networkabnormal:
                        showMsg("当前网络不可用");
                        break;
                    case HttpCode.http_failed:
                        showMsg("连接失败");
                        break;
                    case HttpCode.http_timeout:
                        showMsg("请求超时");
                        break;
                    case HttpCode.LOSTPASSWORD:
                        try {
                            JSONObject json = new JSONObject(msg.obj.toString());
                            if (json.getString("ret").equals("1")) {
                                showMsg("修改成功");
                                finish();
                            } else {
                                showMsg(json.getString("msg"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                }
            }
        };
    }


    /**
     * 数据验证
     * type 1  获取验证码
     * 2  提交数据
     */
    private void juge(int type) {
        phone = viewDelegate.getText(R.id.lost_ed_phoneNumber);
        password = viewDelegate.getText(R.id.lost_ed_password);
        repPassword = viewDelegate.getText(R.id.lost_ed_confirmPassword);
        code = viewDelegate.getText(R.id.lost_ed_verify_code);
        if (RegexUtils.checkPhoneNumber(phone) && !"".equals(phone)) {
            if (!password.equals("")) {
                if (password.equals(repPassword)) {
                    if (type == 1) {
                        getCode();
                    } else {
                        checkCode(code);
                    }
                } else {
                    viewDelegate.setError("密码输入不一致");
                }
            } else {
                viewDelegate.setError("密码不能空");
            }
        } else {
            viewDelegate.setError("手机号输入有误");
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.password_rel:
                //获取验证码
                juge(1);
                break;
            case R.id.lost_bt_confirm:
                //提交数据
                viewDelegate.setError("");
                juge(2);
                break;
            case R.id.iv_back:
                finish();
                break;
        }

    }


    private void lostSubmit() {
        map = new HashMap<String, String>();
        map.put("phone", phone);
        map.put("password", password);
        showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(this, handler, url_lostpassword, map, HttpCode.LOSTPASSWORD);

    }

    //获取短信验证码
    private void getCode() {

        SMSSDK.getInstance().getSmsCodeAsyn(phone, "137715", new SmscodeListener() {
            @Override
            public void getCodeSuccess(final String uuid) {
                showMsg("短信发送成功");
            }

            @Override
            public void getCodeFail(int errCode, final String errmsg) {
                //失败后停止计时
                showMsg("短信发送失败");
                System.out.println("错误代码是：" + errmsg);
            }
        });
    }

    //验证验证码
    private void checkCode(String str_code) {
        if (str_code.equals("")) {
            viewDelegate.setError("验证码不能为空");
            return;
        }
        showProgressDialog("", "加载中，请稍后...");
        SMSSDK.getInstance().checkSmsCodeAsyn(phone, str_code, new SmscheckListener() {
            @Override
            public void checkCodeSuccess(final String code) {
                //验证成功 执行下一步操作
                hideProgressDialog();
                lostSubmit();
            }

            @Override
            public void checkCodeFail(int errCode, final String errmsg) {
                hideProgressDialog();
                //失败
                viewDelegate.setError("验证码输入有误请重新输入");
            }
        });
    }

    @Override
    protected Class<LostPassworddelegate> getDelegateClass() {
        return LostPassworddelegate.class;
    }
}
